/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package xowl.interpreter.frontends.rdfxml;

/**
 *
 * @author Laurent WOUTERS
 */
public class RDFXMLLoader implements xowl.interpreter.frontends.Loader {
    static final String name = "RDF/XML Loader";
    
    private static final String rdfRDF = "rdf:" + xowl.interpreter.frontends.VocRDF.nameRDF;
    
    @Override public xowl.lang.owl2.Ontology loadOntology(xowl.utils.logging.Logger logger, String docName, java.io.Reader reader, xowl.interpreter.frontends.OntologyListener listener) {
        xowl.interpreter.backends.rdf.RDFRepository rdfRepo = new xowl.interpreter.backends.rdf.SimpleRDFRepository(logger);
        xowl.lang.owl2.Ontology ontology = null;
        org.apache.xerces.parsers.DOMParser parser = new org.apache.xerces.parsers.DOMParser();
        try {
            parser.parse(new org.xml.sax.InputSource(reader));
            org.w3c.dom.Document document = parser.getDocument();
            for (int i=0; i!=document.getChildNodes().getLength(); i++) {
                 org.w3c.dom.Node node = document.getChildNodes().item(i);
                 if (node.getNodeType() != org.w3c.dom.Node.ELEMENT_NODE)
                     continue;
                 if (node.getNodeName().equals(rdfRDF)) {
                     GraphLoader loader = new GraphLoader(rdfRepo.getDefaultEvaluator(), listener, rdfRepo.getGraph());
                     loader.load(node);
                     ontology = loader.getOntology();
                     xowl.interpreter.backends.rdf.TranslatorRDF_OWL2 translator = new xowl.interpreter.backends.rdf.TranslatorRDF_OWL2(rdfRepo);
                     for (xowl.lang.owl2.Axiom axiom : translator.translateOntology(rdfRepo.getDefaultEvaluator(), rdfRepo.getGraph(), ontology))
                         listener.onAxiom(ontology, axiom);
                 }
            }
        }
        catch (java.lang.Exception ex) {
            logger.error(name, "Error while loading " + docName + ": " + ex.getMessage(), ex);
            return null;
        }
        return ontology;
    }
    
    @Override public void loadQueries(xowl.utils.logging.Logger logger, String docName, java.io.Reader reader, xowl.interpreter.frontends.QueriesListener listener) {
        throw new java.lang.UnsupportedOperationException();
    }
}
